<?php 
class CategoryController extends Controller{
	public $db;
	public $category;
	public function __init(){
		$this->db=K('category');
		$this->category=$this->db->getList();
	}

	public function index(){		
		$this->assign('category',$this->category);
		$this->display();
	}

	public function add(){
		if(IS_POST){
			if($this->db->addCategory()){
				$this->success('栏目添加成功','index');
			}else{
				$this->error($this->db->error);
			}			
		}else{
			$this->assign('category',$this->category);			
			$this->display();
		}
	}

	public function edit(){
		if(IS_POST){
			if($this->db->editCategory()){
				$this->success('栏目编辑成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$cid=Q('get.cid',0,'intval');
			$field=$this->db->find($cid);
			$category=$this->category;
			foreach ($category as $key => $value) {
				$category[$key]['selected']=$value['cid']==$field['pid']?"selected=''":'';
				$category[$key]['disabled']=Data::isChild($category,$value['cid'],$field['cid'])||$value['cid']==$field['cid']?"disabled=''":'';
			}
			$this->assign('category',$category);
			$this->assign('field',$field);
			$this->display();
		}
	}

	public function delete(){
		$cid=Q('cid',0,'intval');
		if($this->db->deleteCategory($cid)){
			$this->success('栏目删除成功','index');
		}else{
			$this->error($this->db->error);
		}
	}



}

?>